<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Invoice
 *
 * @author Regan
 */
class Invoicedetail extends AppModel {
    public function getInvoiceList($CAR)
    {
        $objlist = $this->query("select DokNo from tblpibdok where CAR = '$CAR' and DokKd = '380'");
        
        foreach($objlist as $obj)
        {
            $res[$obj['tblpibdok']['DokNo']] = $obj['tblpibdok']['DokNo'];
        }
        
        return $res;
    }
    
    public function getDataInvoice($CAR)
    {
        $obj = $this->query("select a.DokNo"
                . ", coalesce(b.InvoiceLevel, '') as InvoiceLevel"
                . ", coalesce(b.ShipRef, '') as ShipRef "
                . "from tblpibdok a "
                . "left join invoicedetails b "
                . "on a.DokNo = b.DokNo "
                . "where a.CAR = '$CAR' and a.DokKd = '380'");
        
        return $obj;
    }
    
    public function RecordSave($invoicesave)
    {
        $checkResult = $this->CheckExisting($invoicesave);
        
        if($checkResult) //if true, berarti record sudah ada, metode update
        {
            $this->updateAll(
                    array(
                        'InvoiceLevel' => "'" . $invoicesave['Invoicedetail']['InvoiceLevel'] . "'",
                        'ShipRef' => "'" . $invoicesave['Invoicedetail']['ShipRef'] . "'" ,
                        'modifiedBy' => "'" . $invoicesave['Invoicedetail']['createdBy'] . "'",
                        'modified' => "'" . date('Y-m-d H:i:s') . "'"
                    ),
                    array(
                        'DokNo' => $invoicesave['Invoicedetail']['DokNo']
                    )
                );
        }
        else
        {
            $this->create();
            $this->save($invoicesave);
        }
    }
    
    private function CheckExisting($invoicesave)
    {
        $res = $this->find('all', array(
            'conditions' => array('DokNo' => $invoicesave['Invoicedetail']['DokNo'])
            ));
        
        if($res)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
